//
//  setDealPaswordController.m
//  SSSAllRollAgency
//
//  Created by Darker on 2017/3/15.
//  Copyright © 2017年 Darker. All rights reserved.
//

#import "setDealPaswordController.h"

@interface setDealPaswordController ()<QMUITextFieldDelegate>

{
    QMUITextField *_paswordTextField;
    QMUITextField *_surePaswordTextField;
}

@end

@implementation setDealPaswordController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    self.title = @"设置交易密码";
    //去掉透明后导航栏下边的黑边
    //    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
    self.navigationController.navigationBar.hidden = NO;
    self.view.backgroundColor = [UIColor colorWithHex:@"F9F9FA"];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setApperance];
    
}
- (void)setApperance{
    
    _paswordTextField = [[QMUITextField alloc]init];
    _paswordTextField.returnKeyType = UIReturnKeyDone;
    _paswordTextField.borderStyle = UITextBorderStyleNone;
    _paswordTextField.font = [SSSFont systemFontOfSize:14];
    _paswordTextField.tintColor = [UIColor colorWithHex:@"44b034"];
    _paswordTextField.placeholder = @"请输入密码";
    _paswordTextField.delegate = self;
    _paswordTextField.secureTextEntry = YES;
    // 设置文本框的清楚按钮的模式
    _paswordTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    // 设置文本框的清楚按钮的模式
    _paswordTextField.clearsOnBeginEditing = YES;
//    [_paswordTextField setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
//    [_paswordTextField setValue:[NSNumber numberWithInt:-15] forKey:@"paddingLeft"];
    [self.view addSubview:_paswordTextField];
    _paswordTextField.sd_layout
    .centerXEqualToView(self.view)
    .topSpaceToView(self.view,64 + HeigthIos6CompeteWithCurrent(36))
    .widthIs(WidthIos6CompeteWithCurrent(291))
    .heightIs(HeigthIos6CompeteWithCurrent(51));
    _paswordTextField.layer.borderWidth = 0.5;
    _paswordTextField.layer.borderColor = [[UIColor grayColor]colorWithAlphaComponent:0.3].CGColor;
    _paswordTextField.layer.cornerRadius = HeigthIos6CompeteWithCurrent(10);
    _paswordTextField.layer.masksToBounds = YES;
    
    
    
    //确认密码
    _surePaswordTextField = [[QMUITextField alloc]init];
    _surePaswordTextField.tintColor = [UIColor colorWithHex:@"44b034"];
    _surePaswordTextField.borderStyle = UITextBorderStyleNone;
    _surePaswordTextField.delegate = self;
    _surePaswordTextField.placeholder = @"再次输入密码";
    _surePaswordTextField.font = [SSSFont systemFontOfSize:14];
    _paswordTextField.layer.borderColor = [[UIColor grayColor]colorWithAlphaComponent:0.3].CGColor;
    _paswordTextField.returnKeyType = UIReturnKeyDone;
    // 设置文本框的清楚按钮的模式
    _paswordTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    // 设置文本框的清楚按钮的模式
    _paswordTextField.clearsOnBeginEditing = YES;
    _surePaswordTextField.secureTextEntry = YES;
    [self.view addSubview:_surePaswordTextField];
    _surePaswordTextField.sd_layout
    .leftEqualToView(_paswordTextField)
    .topSpaceToView(_paswordTextField,HeigthIos6CompeteWithCurrent(19))
    .rightEqualToView(_paswordTextField)
    .heightIs(HeigthIos6CompeteWithCurrent(51));
    _surePaswordTextField.layer.borderWidth = 0.5;
    _surePaswordTextField.layer.borderColor = [[UIColor grayColor]colorWithAlphaComponent:0.3].CGColor;
    _surePaswordTextField.layer.cornerRadius = HeigthIos6CompeteWithCurrent(10);
    _surePaswordTextField.layer.masksToBounds = YES;
    
    //确定按钮
    UIButton *sureBtn = [[UIButton alloc]init];
    [sureBtn setTitle:@"确 定" forState:UIControlStateNormal];
    [sureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [sureBtn setBackgroundColor:[UIColor colorWithHex:@"46B77D"]];
    sureBtn.titleLabel.font = [SSSFont systemFontOfSize:20];
    [sureBtn addTarget:self action:@selector(sureBtnClick) forControlEvents:UIControlEventTouchDown];
    [self.view addSubview:sureBtn];
    sureBtn.sd_layout
    .topSpaceToView(_surePaswordTextField,HeigthIos6CompeteWithCurrent(40))
    .leftEqualToView(_paswordTextField)
    .rightEqualToView(_paswordTextField)
    .heightIs(HeigthIos6CompeteWithCurrent(51));
    sureBtn.layer.cornerRadius = HeigthIos6CompeteWithCurrent(10);
    sureBtn.layer.masksToBounds = YES;
    
    //取消按钮
    UIButton *cancelBtn = [[UIButton alloc]init];
    [cancelBtn setTitle:@"取 消" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:[UIColor colorWithHex:@"46B77D"] forState:UIControlStateNormal];
    [cancelBtn setBackgroundColor:[UIColor clearColor]];
    cancelBtn.titleLabel.font = [SSSFont systemFontOfSize:20];
    [cancelBtn addTarget:self action:@selector(cancelBtnClick) forControlEvents:UIControlEventTouchDown];
    [self.view addSubview:cancelBtn];
    cancelBtn.sd_layout
    .topSpaceToView(sureBtn,HeigthIos6CompeteWithCurrent(20))
    .leftEqualToView(_paswordTextField)
    .rightEqualToView(_paswordTextField)
    .heightIs(HeigthIos6CompeteWithCurrent(51));
    cancelBtn.layer.cornerRadius = HeigthIos6CompeteWithCurrent(10);
    cancelBtn.layer.masksToBounds = YES;
    cancelBtn.layer.borderColor = [UIColor colorWithHex:@"46B77D"].CGColor;
    cancelBtn.layer.borderWidth = 0.5;

}
- (void)sureBtnClick{
    
    if (_paswordTextField.text.length==0) {
        showTipHudStr(@"请输入密码");
        return;
    }
    if (_surePaswordTextField.text.length==0) {
         showTipHudStr(@"请再次输入密码");
        return;
    }
    if (_paswordTextField.text.length<6) {
        showTipHudStr(@"密码不少于六位数");
        return;

    }
    if (_paswordTextField.text.length>20) {
        showTipHudStr(@"密码不多于15位数");
        return;
    }

    if (![SSSRegularExpression checkPassword:_paswordTextField.text]) {
        showTipHudStr(@"密码只包含字母和数字");
        return;
    }
    if (![_paswordTextField.text isEqualToString:_surePaswordTextField.text]){
        showTipHudStr(@"两次输入密码不一致");
        return;
        
    }
   [[XHRequestManager manager] postRequest:kConfigPath(kSettingTradeCode) params:@{@"transactionPwd":_paswordTextField.text} success:^(id  _Nonnull responseObj) {
       
       if (kResponseStatus==0) {
           [QMUITips showSucceed:@"设置交易密码成功" inView:kKeyWindow hideAfterDelay:1];
           dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
               [self.navigationController popViewControllerAnimated:YES];
           });
           
       }
       else showTipHudStr(kResponseMessage);

       
   } failure:^(NSError * _Nonnull error) {
       
       [QMUITips showError:error.localizedDescription inView:self.view hideAfterDelay:2];

   }];
    
//    [ZDNetManager zd_requestWithType:ZDHttpRequestTypePost UrlString:kConfigPath(kSettingTradeCode) Parameters:@{@"transactionPwd":_paswordTextField.text} SuccessBlock:^(id responseObj) {
//        if (kResponseStatus==0) {
//            [QMUITips showSucceed:@"设置交易密码成功" inView:kKeyWindow hideAfterDelay:1];
//            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//                [self.navigationController popViewControllerAnimated:YES];
//            });
//
//        }
//        else showTipHudStr(kResponseMessage);
//       
//    } FailureBlock:^(NSError *error) {
//          } progress:nil];
    
}
- (void)cancelBtnClick{
    
    
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [_surePaswordTextField resignFirstResponder];
    [_paswordTextField resignFirstResponder];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)netFor{


}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
